﻿using UnityEngine;
using System.Collections;

public class AttackAndDamage : MonoBehaviour {

    public AudioClip bossAudioDeath;

    public AudioClip monstAudioDeath;

    public AudioClip playerAudioDeath;

    public float hp = 1000;

    // 普通攻击血量
    public float normalAttack = 80;

    // 攻击距离
    public float attackDistance = 2;

    protected Animator animator;

    protected void Awake()
    {
        animator = GetComponent<Animator>();

      
    }

    public virtual void TakenDamage(float damage)
    {
        if (hp > 0)
        {
            // 判断血量
            hp -= damage;
        }

            if (hp > 0)
            {
                if (this.tag == Tags.boss || this.tag == Tags.monst)
                {
                    animator.SetTrigger("MonDamage");
                }
            }
            else
            {
                if (this.tag == Tags.boss || this.tag == Tags.monst)
                {
                    animator.SetTrigger("MonDeath");

                    if (this.tag.Equals(Tags.boss))
                    {
                        AudioSource.PlayClipAtPoint(bossAudioDeath, transform.position, 1.0f);
                    }
                    else
                        if (this.tag.Equals(Tags.monst))
                        { AudioSource.PlayClipAtPoint(monstAudioDeath, transform.position, 1.0f); }

                }
                else if(this.tag== Tags.player)
                {
                    AudioSource.PlayClipAtPoint(playerAudioDeath, transform.position, 1.0f);
                    animator.SetTrigger("Death");
                }

                EnemyManager._instance.enemyListArray.Remove(this.gameObject);

                Destroy(this.gameObject,1.0f);

                if (this.tag == Tags.boss || this.tag == Tags.monst)
                BoomSphere();  // 爆装备

                this.GetComponent<CharacterController>().enabled=false;

            }

            if (this.tag == Tags.boss || this.tag == Tags.player)
            {
                GameObject.Instantiate(Resources.Load("HitBoss"),transform.position + Vector3.up,transform.rotation);
            }
            else
                if (this.tag == Tags.monst)
                {
                    GameObject.Instantiate(Resources.Load("HitMonster"), transform.position + Vector3.up, transform.rotation);
                }

 
    }

    void BoomSphere()
    {
        int count = Random.Range(0,2);
        for (int i = 0; i < count; i++)
        {
            int index = Random.Range(0, 2);
            if (index == 0)
            {
                GameObject.Instantiate(Resources.Load("GunSphere"), transform.position, transform.rotation);
            }
            else
                if (index == 1)
                {
                    GameObject.Instantiate(Resources.Load("SwordSphere"), transform.position, transform.rotation);
                }

        }


    }


}
